/**
 * 
 */
package com.aat.dxfy.thread.safe1;

/**
 * @date 2016年6月23日 下午1:11:29
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 功能是：
 * @info java spring
 * @update none
 */
public class Test02 {

	public static void main(String[] args) {
		int tickets = 100;
		SaleTcket1 s1 = new SaleTcket1(tickets);

		Thread t1 = new Thread(s1, "一号窗口");
		Thread t2 = new Thread(s1, "二号窗口");
		Thread t3 = new Thread(s1, "三号窗口");
		Thread t4 = new Thread(s1, "四号窗口 ");
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		
	}

}

class SaleTcket1 implements Runnable {

	private int tickets = 0;

	public SaleTcket1(int tickets) {
		this.tickets = tickets;
	}

	public void run() {
		while (tickets > 0) {
			sale();
		}
	}

	private synchronized void sale() {
		if (tickets > 0) {
			System.out.println(Thread.currentThread().getName() + "卖出 第" + (tickets--) + "张票");
		}

		try {
			
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
}